package com.yc.menu.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.api.CommonPage;
import com.yc.api.CommonResult;
import com.yc.base.BaseController;
import com.yc.menu.entity.Menu;
import com.yc.menu.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-12-09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/menu")
@Api(value = "MenuController", tags = "后台菜单表")
public class MenuController extends BaseController {

    private final IMenuService menuService;

    @PostMapping("/add")
    @ApiOperation(value = "创建Menu", notes = "创建Menu")
    @SaCheckPermission(value = {"management:menu:add"})
    public CommonResult<?> create(@RequestBody Menu menu) {
        boolean isOk = menuService.create(menu);
        if (isOk) {
            return CommonResult.success(menu);
        }
        return CommonResult.failed();
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "删除Menu", notes = "删除Menu")
    @SaCheckPermission(value = {"management:menu:delete"})
    public CommonResult<?> deleteByIds(@RequestBody List<Long> ids) {
        boolean isOk = menuService.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @ApiOperation(value = "更新Menu", notes = "更新Menu")
    @SaCheckPermission(value = {"management:menu:update"})
    public CommonResult<?> updateById(@PathVariable("id") long id, @RequestBody Menu menu) {
        menu.setId(id);
        menu.setParentId(ObjectUtil.isNull(menu.getParentId()) ? 0 : menu.getParentId());
        boolean isOk = menuService.update(menu);
        if (isOk) {
            return CommonResult.success(menu);
        }
        return CommonResult.failed();
    }

    @PostMapping("/queryById/{id}")
    @ApiOperation(value = "根据Id获取Menu", notes = "根据Id获取Menu")
    public CommonResult<?> queryById(@PathVariable("id") Long id) {
        Menu menu = menuService.queryById(id);
        if (menu != null) {
            return CommonResult.success(menu);
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取Menu列表", notes = "获取Menu列表")
    public CommonResult<?> listAll() {
        List<Menu> list = menuService.selectList();
        return CommonResult.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取Menu列表", notes = "获取Menu列表")
    public CommonResult<?> pageAll(@RequestBody JSONObject params) {
        int pageNum = params.getInt("pageNum", 1);
        int pageSize = params.getInt("pageSize", 25);
        IPage<Menu> page = menuService.selectPage(new Page<Menu>(pageNum, pageSize));
        return CommonResult.success(CommonPage.restPage(page));
    }
}
